home *** CD-ROM | disk | FTP | other *** search
/ Aminet 2 / Aminet AMIGA CDROM (1994)(Walnut Creek)[Feb 1994][W.O. 44790-1].iso / Aminet / gfx / misc / lise20.lha / lise2.0 / mdl / src / mdl.c next >
C/C++ Source or Header  |  1993-03-31  |  27KB  |  912 lines

  1. /* {(rm mdl ; cc -DUNIX -I. -s -n -O -Wc,-Nd4000 -Wc,-Ns4000 -Wc,-Nt50000 -DSYSV -DNLS16 -Dhp9000s300 -o mdl mdl.c mdllib.o  -L /usr/lib/X11R4 -L /usr/lib/Motif1.1 -lMrm -lXm -lXt -lX11 -lPW ; mdl test.mdl)& } */
  2. /**---------------------------------------------------------------------
  3. ***    
  4. ***   file:      mdl.c
  5. ***
  6. ***   project:   Menu Description Language
  7. ***                     General purpose Menu generator.
  8. ***
  9. ***         (c) 1990 Rainer Kowallik
  10. ***
  11. ***-------------------------------------------------------------------*/
  12.  
  13. #include <stdio.h>
  14. #include <sys/stat.h>
  15. #define TMPDIRFILE "ram:mdltmpdir.tmp"
  16.  
  17. #ifdef AMIGA
  18. #include <exec/types.h>
  19. #include <exec/types.h>
  20. #include <libraries/dos.h>
  21. #include <libraries/dosextens.h>
  22. #include <intuition/intuition.h>
  23. #include <workbench/workbench.h>
  24. #include <workbench/startup.h>
  25. #include <workbench/icon.h>
  26.  
  27. extern struct WBStartup *WBenchMsg;
  28. extern struct IconBase *IconBase;
  29. #endif
  30. #ifdef UNIX
  31. #include <unistd.h>
  32. #endif
  33.  
  34. #include <mdllib.h>
  35.  
  36. int    tmpnum=0;
  37. char   resource[80];
  38. char   *fn_command_str[160];
  39. char   *var_name_str[160];
  40. char   *var_value_str[160];
  41. int    var_value_int[160];
  42. char   language[80];
  43. char   myname[80];
  44. int    wl = -1;
  45. int    wt = -1;
  46.  
  47. void tmpnam_(s)      /* due to a bug in the Lattice C 5.0 library ! */
  48. char *s;
  49. {
  50. long int t;
  51.  
  52.    time(&t); t = t + tmpnum++;
  53.    sprintf(s,"tmp%x",t);
  54. }
  55.  
  56. /* --------------------------------------------------------
  57.    Special callback function for mdl, executes string in
  58.    fn_command_str[last_gadgetid]
  59.    -------------------------------------------------------- */
  60. void CallBack_mdl()
  61. {
  62. parse(fn_command_str[last_gadgetid]);
  63. }
  64.  
  65.  
  66. void main(argc,argv)
  67. unsigned int argc;
  68. char *argv[];
  69. {
  70. int n;
  71. FILE *stream;
  72. char mdlfile[80];
  73. char c,s[80];
  74. char cmdline[256];
  75. FILE *tmp1;
  76.  
  77. #ifdef AMIGA
  78. struct WBArg *Arg;
  79. #endif
  80.  
  81.  
  82.    strcpy(language,"ksh ");
  83.    strcpy(mdlfile,argv[1]);
  84.    if(argc > 0) strcpy(myname,argv[0]);
  85.  
  86.  
  87.  
  88. #ifdef AMIGA
  89.    if(argc <= 1) {
  90.       IconBase = (struct IconBase *) OpenLibrary("icon.library", 0L);
  91.       Arg = WBenchMsg->sm_ArgList;
  92.       strcpy(myname,Arg->wa_Name);
  93.       Arg++;
  94.       CurrentDir(Arg->wa_Lock);
  95.       strcpy(mdlfile,Arg->wa_Name);
  96.    }
  97. #endif
  98.  
  99.    stream = fopen(mdlfile,"r");
  100.    if(stream == NULL) {
  101.       fprintf(stderr,"mdl: could not open command file>%s<\n",mdlfile);
  102.       exit(0);
  103.    }
  104.  
  105. /* the following is for two purposes: 
  106.    1. Making a self detaching programm on an Amiga
  107.    2. passing standard commandstrings from the mdl file
  108. */
  109.  
  110.    strcpy(cmdline,"");
  111.    c = fgetc(stream);         /* an '-' in the first column of the first line */
  112.    ungetc(c,stream);
  113.    if(c == '-') {             /* signals a default command string */
  114.       fgets(cmdline,80,stream); /* read default command string */
  115.    }
  116.    if((!checkopt(argc,argv,"-fork",s)) && (argc > 0)) {
  117.       strcpy(s,cmdline);
  118.       strcpy(cmdline," ");
  119.       strcat(cmdline,myname); strcat(cmdline," ");
  120.       for(n = 1; n < argc; n++) {
  121.          strcat(cmdline,argv[n]);
  122.          strcat(cmdline," ");
  123.       }
  124.       strcat(cmdline,"-fork ");
  125.       strcat(cmdline,s);            /* add default options from mdl file */
  126. #ifdef UNIX
  127.       cmdline[0] = '(';
  128.       strcat(cmdline,")&");
  129.       system(cmdline);
  130. #endif
  131. #ifdef AMIGA
  132.       amifork(cmdline);
  133. #endif
  134.       fclose(stream);
  135.       exit(0);
  136.    }
  137.  
  138.  
  139. #ifdef UNIX
  140.    strcpy(resource,mdlfile);  /* default resource file name is the mdl prog name */
  141.    c = fgetc(stream);
  142.    if(c == '{') { /* } create resource file: copy text enclosed in {} to TMPFILE */
  143.       strcpy(resource,"mdl.tmp");
  144.       sprintf(s,"/usr/lib/X11/app-defaults/%s",resource);
  145.       tmp1 = fopen(s,"w");
  146.       chmod(s,0666);
  147.       n=1;
  148.       while(!feof(stream)) {
  149.          c = fgetc(stream);
  150.          if(c == '{') n++;
  151.          if(c == '}') n--;
  152.          if(n == 0) break;
  153.          fputc(c,tmp1);
  154.       }
  155.       fclose(tmp1);
  156.    } else {
  157.       ungetc(c,stream);
  158.       sprintf(s,"/usr/lib/X11/app-defaults/%s",resource);
  159.       tmp1 = fopen(s,"r");          /* check if resource file exists */
  160.       if(tmp1 == NULL) {
  161.          strcpy(resource,"mdl");    /* take mdl default resource file instead */
  162.       } else {
  163.          fclose(tmp1);
  164.       }
  165.    }
  166.  
  167. /* open display and read resource file (if any) */
  168.  
  169.    XtToolkitInitialize();
  170.    app_context = XtCreateApplicationContext();
  171.    if(!app_context) {
  172.       printf("could not create application context\n");
  173.       exit(0);
  174.    }
  175.    display = XtOpenDisplay (app_context, NULL, myname, resource, NULL, 0,
  176.                  &argc, argv);
  177.                  
  178.     if (!display) {
  179.         XtWarning ("mdl: Can't open display, exiting...");
  180.         exit (0);
  181.     }
  182. #endif
  183.  
  184.   menu_init(argv,stream);
  185. /* !!!! Debug !!!!
  186.    sprintf(s,"fn_number = %d\n",fn_number);
  187.    Help(s);
  188.    !!!! Debug !!!! */
  189.   fclose(stream);
  190.  
  191. #ifdef AMIGA
  192.   my_window = (struct Window *) OpenWindow( my_new_window );
  193.   SetMenuStrip( my_window, menu_bar );
  194.   if(my_window == NULL)
  195.   {
  196.     fprintf(stderr,"could not open Window\n");
  197.     close_gfx(my_window);
  198.     exit(0);  
  199.   }
  200.   fin_flg = FALSE;
  201.   MainLoop(my_window);
  202. #endif
  203.  
  204. #ifdef UNIX
  205. /* dont forget to activate the last menu entry */
  206.    add_item(-1,-1,NEW_MENU,"empty",0,0);
  207.     XtRealizeWidget (app_shell);
  208.     XtAppMainLoop (app_context);
  209. #endif
  210.  
  211.   exit(0);
  212. }
  213.  
  214.  
  215.  
  216. /* ********************************************************************
  217.  
  218.    S T R I N G H A N D L I N G
  219.  
  220.    ******************************************************************** */
  221.  
  222. /* --------------------------------------------------
  223.    reurns in s1 s2 from nth occurence of c up to c
  224.    -------------------------------------------------- */
  225.  
  226. ExtractStr(s1,s2,n,c)
  227. char s1[],s2[],c;
  228. int n;
  229. {
  230. int m,i,l;
  231. char c1;
  232.  
  233.    m=0; l=strlen(s2);
  234.    i = 0;
  235.    if(n > 0) {
  236.       for(i=0;i<l;i++) {
  237.           c1=s2[i];
  238.           if(c1 == c) m++;
  239.           if(m == n) break;
  240.       }
  241.       s1[0] = 0;
  242.       if(c1 != c) return(-1);
  243.       i++;
  244.    }
  245.    m=0;
  246.    while(i<l) {
  247.       c1 = s2[i++];
  248.       if(c1 == c) break;
  249.       s1[m++] = c1;
  250.    }
  251.    s1[m] = 0;
  252.    if(strlen(s1)==0) return(-1);
  253.    return(1);
  254. }
  255.  
  256. /* --------------------------------------------------
  257.    check if character is a bra '([{' or a ket ')]}'
  258.    -------------------------------------------------- */
  259. tstbra(c)
  260. char c;
  261. {
  262.    if((c == '(') || (c == '[') || (c == '{')) return(1);
  263.    if((c == ')') || (c == ']') || (c == '}')) return(-1);
  264.    return(0);
  265. }
  266.  
  267.  
  268.  
  269.  
  270. /* --------------------------------------------------
  271.     get parameter enclosed in brackets from file.
  272.     any New line character will be substituted by the
  273.     supplied character.
  274.    -------------------------------------------------- */
  275. fgetpar(stream,s,nl)
  276. FILE *stream;
  277. char nl,s[];
  278. {
  279. int n,cnt;
  280. char c,bra,ket;
  281.  
  282.    n=0;
  283.    while(n != 1) {
  284.       if(feof(stream)) return(0);
  285.       c = fgetc(stream);
  286.       n = tstbra(c);
  287.    }
  288.    bra = c;
  289.    if(bra == '(') {
  290.       ket = ')';
  291.    } else {
  292.       ket = bra + 2;
  293.    }
  294. /* skip SPC and NL at start of line */
  295.    c = 0;
  296.    while(c < 33) c = fgetc(stream);
  297.    ungetc(c,stream);
  298.    n = 0; cnt = 1;
  299.    while(!feof(stream)) {
  300.       c = fgetc(stream);
  301.       if(c == bra) cnt = cnt + 1;
  302.       if(c == ket) cnt = cnt - 1;
  303.       if(c == 10) {s[n++] = ' ' ; c = nl; }
  304.       if(cnt == 0) break;
  305.       s[n++] = c;
  306.    }
  307.    s[n] = 0; 
  308. }
  309.  
  310. /* --------------------------------------------------
  311.     get parameter enclosed in brackets from string.
  312.    -------------------------------------------------- */
  313. sgetpar(str,s,n,j)
  314. char str[],s[];
  315. int n,*j;
  316. {
  317. int m,i,l,cnt;
  318. unsigned char c,bra,ket;
  319.  
  320.    l = strlen(str);
  321.    i = *j;
  322.    i = n; c=str[i];
  323.    bra = 0; ket=' ';
  324.    if(tstbra(c) == 1) {   
  325.       bra = c;
  326.       if(bra == '(') {
  327.         ket = ')';
  328.       } else {
  329.         ket = bra + 2;
  330.       }
  331.       i = i + 1;
  332.    }
  333.    m = 0; cnt = 1;
  334.    while(m < l) {
  335.      c = str[i++];
  336.      if(c == bra) cnt = cnt + 1;
  337.      if(c == ket) cnt = cnt - 1;
  338.      if((ket == ' ') && (tstbra(c) != 0)) cnt = cnt -1;
  339.      if((ket == ' ') && (c < '0')) cnt = cnt -1;
  340.      if(cnt <= 0) break;
  341.      s[m++] = c;
  342.    }
  343.    if(tstbra(c)==-1) i++;
  344.    s[m] = 0;
  345.    *j = i-1;
  346.    return(0);
  347. }
  348.  
  349.  
  350. /* ------------------------------------------------------
  351.    reading file and start initializing menus
  352.    ------------------------------------------------------ */
  353. menu_init(argv,stream)
  354. char *argv[];
  355. FILE *stream;
  356. {
  357. int n,i;
  358. int p1,p2;
  359. int x = 200,
  360.     y = 200,
  361.     set_flg = 0;
  362. float fx, fy;
  363. char c,s[256],s1[128],s2[128],*cmd,title[80];
  364. #ifdef UNIX
  365. Arg args[10];
  366. #endif
  367.    cmd = (char *) malloc(8192);
  368.    fx = 1.0; fy = 1.0;
  369.  
  370.    strcpy(title,"UNTITLED SHOULD CHANGE IT !");
  371.    while(!feof(stream)) {         /* go through file */
  372.       c=0;
  373.       while(c < 33) {            /* skip NL CR SPC TAB ... */
  374.          c = fgetc(stream);
  375.          if(feof(stream)) {free(cmd); return(0);}
  376.       }
  377.       ungetc(c,stream);
  378.       c = 33; s[0]=0; n=0;
  379.       while((c > 32) && (c < 123)) {      /* read menu command */
  380.          c = toupper(fgetc(stream));
  381.          if((c == '(') || (c == '[') || (c == ' ') || (c == '{')) break;
  382.          s[n++] = c;
  383.       }
  384.       ungetc(c,stream);
  385.       s[n] = 0;
  386. /* ----------- parse command and read following parameters ---------------- */
  387.       if(strcmp(s,"SIZE") == 0) {      /* size of main window */
  388.          fgetpar(stream,s,' ');
  389.          ExtractStr(s1,s,0,',');      /* get x size */
  390.          x = (int) (((float) atoi(s1)) * fx);
  391.          ExtractStr(s1,s,1,',');      /* get y size */
  392.          y = (int) (((float) atoi(s1)) * fy);
  393.          n = ExtractStr(s1,s,2,',');      /* get y increment */
  394.          if(n == 1) bas_y = atoi(s1);
  395.          n = ExtractStr(s1,s,3,',');      /* get top edge */
  396.          if(n == 1) wt = atoi(s1);
  397.          n = ExtractStr(s1,s,4,',');      /* get left edge */
  398.          if(n == 1) wl = atoi(s1);
  399.          continue;
  400.       }
  401.       if(strcmp(s,"NAME") == 0) {      /* name of application */
  402.          fgetpar(stream,s,' ');
  403.          strcpy(title,s);
  404.          continue;
  405.       }
  406.       if(strcmp(s,"FACTOR") == 0) {   /* scaling factor */
  407.          fgetpar(stream,s,' ');
  408.          ExtractStr(s1,s,0,',');      /* get x size */
  409.          sscanf(s1,"%f",&fx);
  410.          ExtractStr(s1,s,1,',');      /* get y size */
  411.          sscanf(s1,"%f",&fy);
  412.          continue;
  413.       }
  414.       if(strcmp(s,"LANGUAGE") == 0) {  /* command language for application */
  415.          fgetpar(stream,s,' ');
  416.          strcpy(language,s);
  417.          continue;
  418.       }
  419.       if(set_flg == 0) {         /* open window */
  420.          set_flg = 1;            /* should be done only once */
  421.    /*   Create application shell.
  422.    */
  423.  
  424. #ifdef AMIGA
  425.          my_new_window = init_window(x,y,title,0,1);
  426.          if(wl > 0)my_new_window->LeftEdge = wl;
  427.          if(wt > 0)my_new_window->TopEdge = wt;
  428. #endif
  429. #ifdef UNIX
  430.     n = 0;
  431.     XtSetArg (args[n], XmNwidth, x);  n++;
  432.     XtSetArg (args[n], XmNheight, y);  n++;
  433.     app_shell = XtAppCreateShell (myname,resource,
  434.                 applicationShellWidgetClass, display, args, n);
  435.     main_window = (Widget) CreateApplication (app_shell);
  436. #endif
  437.       }
  438.       if(strcmp(s,"STARTUP") == 0) {  /* do initializing procedure */
  439.          fgetpar(stream,cmd,10);
  440.          parse(cmd);
  441.          continue;
  442.       }
  443.       if(strcmp(s,"NEW_MENU") == 0) {      /* new entry on menu bar */
  444.          fgetpar(stream,s,' ');         /* get name */
  445.          add_item(-1,-1,NEW_MENU,s,0,0);
  446.          fn_number++;
  447.          continue;
  448.       }
  449.       if(strcmp(s,"MENU") == 0) {      /* new menu item */
  450.          fgetpar(stream,s,' ');         /* get name */
  451.          fgetpar(stream,cmd,10);      /* get action */
  452.          add_item(-1,-1,MENU,s,0,0);
  453.          fn_command[fn_number] = (void *)CallBack_mdl;
  454.          fn_command_str[fn_number] = (char *) malloc(strlen(cmd)+1);
  455.          strcpy(fn_command_str[fn_number],cmd);
  456.          fn_number++;
  457.          continue;
  458.       }
  459.       if(strcmp(s,"RADIO") == 0) {      /* start radio box */
  460.          add_item(-1,-1,RADIO,"radio_box",0,0);
  461.          fn_number++;
  462.          continue;
  463.       }
  464.       if(strcmp(s,"TOGGLE") == 0) {      /* toggle button */
  465.          fgetpar(stream,s,' ');         /* get name and position */
  466.          fgetpar(stream,cmd,10);      /* get variable name */
  467.          ExtractStr(s1,s,0,',');
  468.          x = (int) (((float) atoi(s1)) * fx); /* get x position */
  469.          ExtractStr(s1,s,1,',');
  470.          y = (int) (((float) atoi(s1)) * fy); /* get y position */
  471.          ExtractStr(s1,s,2,',');       /* get name */
  472.          add_item(x,y,TOGGLE,s1,0,0);
  473.          var_name_str[fn_number] = (char *)malloc(strlen(cmd)+1);
  474.          strcpy(var_name_str[fn_number],cmd);
  475.          var_value[fn_number] = &var_value_int[fn_number];
  476.          fn_number++;
  477.          continue;
  478.       }
  479.       if(strcmp(s,"PUSH") == 0) {      /* push button */
  480.          fgetpar(stream,s,' ');         /* get name and position */
  481.          fgetpar(stream,cmd,10);      /* get command string */
  482.          ExtractStr(s1,s,0,',');
  483.          x = (int) (((float) atoi(s1)) * fx); /* get x position */
  484.          ExtractStr(s1,s,1,',');
  485.          y = (int) (((float) atoi(s1)) * fy); /* get y position */
  486.          ExtractStr(s1,s,2,',');       /* get name */
  487.          add_item(x,y,PUSH,s1,0,0);
  488.          fn_command[fn_number] = (void *)CallBack_mdl;
  489.          fn_command_str[fn_number] = (char *) malloc(strlen(cmd)+1);
  490.          strcpy(fn_command_str[fn_number],cmd);
  491.          fn_number++;
  492.          continue;
  493.       }
  494.       if(strcmp(s,"SCALEX") == 0) {      /* scale */
  495.          fgetpar(stream,s,' ');         /* get name and position */
  496.          fgetpar(stream,cmd,10);      /* get variable name */
  497.          ExtractStr(s1,s,0,',');
  498.          x = (int) (((float) atoi(s1)) * fx); /* get x position */
  499.          ExtractStr(s1,s,1,',');
  500.          y = (int) (((float) atoi(s1)) * fy); /* get y position */
  501.          ExtractStr(s2,s,2,',');       /* get name */
  502.          ExtractStr(s1,s,3,',');
  503.          p1 = (int) (((float) atoi(s1)) * fx); /* get x position */
  504.          ExtractStr(s1,s,1,',');
  505.          p2 = (int) (((float) atoi(s1)) * fy); /* get y position */
  506.          add_item(x,y,SCALEX,s2,p1,p2);
  507.          var_value[fn_number] = &var_value_int[fn_number];
  508.          var_name_str[fn_number] = (char *)malloc(strlen(cmd)+1);
  509.          strcpy(var_name_str[fn_number],cmd);
  510.          fn_number++;
  511.          continue;
  512.       }
  513.       if(strcmp(s,"SCALEY") == 0) {      /* scale */
  514.          fgetpar(stream,s,' ');         /* get name and position */
  515.          fgetpar(stream,cmd,10);      /* get variable name */
  516.          ExtractStr(s1,s,0,',');
  517.          x = (int) (((float) atoi(s1)) * fx); /* get x position */
  518.          ExtractStr(s1,s,1,',');
  519.          y = (int) (((float) atoi(s1)) * fy); /* get y position */
  520.          ExtractStr(s2,s,2,',');       /* get name */
  521.          ExtractStr(s1,s,3,',');
  522.          p1 = (int) (((float) atoi(s1)) * fx); /* get x position */
  523.          ExtractStr(s1,s,1,',');
  524.          p2 = (int) (((float) atoi(s1)) * fy); /* get y position */
  525.          add_item(x,y,SCALEY,s2,p1,p2);
  526.          var_value[fn_number] = &var_value_int[fn_number];
  527.          var_name_str[fn_number] = (char *)malloc(strlen(cmd)+1);
  528.          strcpy(var_name_str[fn_number],cmd);
  529.          fn_number++;
  530.          continue;
  531.       }
  532.       if(strcmp(s,"SCALEX11") == 0) {      /* scale */
  533.          fgetpar(stream,s,' ');         /* get name and position */
  534.          fgetpar(stream,cmd,10);      /* get variable name */
  535.          ExtractStr(s1,s,0,',');
  536.          x = (int) (((float) atoi(s1)) * fx); /* get x position */
  537.          ExtractStr(s1,s,1,',');
  538.          y = (int) (((float) atoi(s1)) * fy); /* get y position */
  539.          ExtractStr(s2,s,2,',');       /* get name */
  540.          ExtractStr(s1,s,3,',');
  541.          p1 = (int) (((float) atoi(s1)) * fx); /* get x position */
  542.          ExtractStr(s1,s,1,',');
  543.          p2 = (int) (((float) atoi(s1)) * fy); /* get y position */
  544.          add_item(x,y,SCALEX11,s2,p1,p2);
  545.          var_value[fn_number] = &var_value_int[fn_number];
  546.          var_name_str[fn_number] = (char *)malloc(strlen(cmd)+1);
  547.          strcpy(var_name_str[fn_number],cmd);
  548.          fn_number++;
  549.          continue;
  550.       }
  551.       if(strcmp(s,"SCALEY11") == 0) {      /* scale */
  552.          fgetpar(stream,s,' ');         /* get name and position */
  553.          fgetpar(stream,cmd,10);      /* get variable name */
  554.          ExtractStr(s1,s,0,',');
  555.          x = (int) (((float) atoi(s1)) * fx); /* get x position */
  556.          ExtractStr(s1,s,1,',');
  557.          y = (int) (((float) atoi(s1)) * fy); /* get y position */
  558.          ExtractStr(s2,s,2,',');       /* get name */
  559.          ExtractStr(s1,s,3,',');
  560.          p1 = (int) (((float) atoi(s1)) * fx); /* get x position */
  561.          ExtractStr(s1,s,1,',');
  562.          p2 = (int) (((float) atoi(s1)) * fy); /* get y position */
  563.          add_item(x,y,SCALEY11,s2,p1,p2);
  564.          var_value[fn_number] = &var_value_int[fn_number];
  565.          var_name_str[fn_number] = (char *)malloc(strlen(cmd)+1);
  566.          strcpy(var_name_str[fn_number],cmd);
  567.          fn_number++;
  568.          continue;
  569.       }
  570.       if(strcmp(s,"SELECTION") == 0) {      /* selection box */
  571.          fgetpar(stream,s,' ');         /* get name and position */
  572.          ExtractStr(s1,s,0,',');
  573.          x = (int) (((float) atoi(s1)) * fx); /* get x position */
  574.          ExtractStr(s1,s,1,',');
  575.          y = (int) (((float) atoi(s1)) * fy); /* get y position */
  576.          n = 2; i = 0; s1[0] = 0;
  577.          while(i != -1) {
  578.             i = ExtractStr(s2,s,n++,',');    /* get name */
  579.             if(i == 1) {
  580.                if(s1[0] != 0) strcat(s1,",");
  581.                strcat(s1,s2);
  582.             }
  583.          }
  584.          add_item(x,y,SELECTION,s1,0,0);
  585.          var_value_str[fn_number] = (char *)malloc(80);
  586.          var_value[fn_number] = (int *)var_value_str[fn_number];
  587.          fn_command[fn_number] = (void *)CallBack_mdl;
  588.          fgetpar(stream,s2,' ');      /* get variable name */
  589.          fgetpar(stream,cmd,10);      /* get command string */
  590.          fn_command_str[fn_number] = (char *) malloc(strlen(cmd)+1);
  591.          strcpy(fn_command_str[fn_number],cmd);
  592.          var_name_str[fn_number] = (char *)malloc(strlen(s2)+1);
  593.          strcpy(var_name_str[fn_number],s2);
  594.          fn_number++;
  595.          continue;
  596.       }
  597.       if(strcmp(s,"FILE_SELECT") == 0) {   /* selection box */
  598.          fgetpar(stream,s,' ');         /* get name and position */
  599.          fgetpar(stream,s2,' ');      /* get variable name */
  600.          fgetpar(stream,cmd,10);      /* get command string */
  601.          ExtractStr(s1,s,0,',');
  602.          x = (int) (((float) atoi(s1)) * fx); /* get x position */
  603.          ExtractStr(s1,s,1,',');
  604.          y = (int) (((float) atoi(s1)) * fy); /* get y position */
  605.          ExtractStr(s1,s,2,',');       /* get name */
  606.          add_item(x,y,FILE_SELECT,s1,0,0);
  607.          var_value_str[fn_number] = (char *)malloc(80);
  608.          var_value[fn_number] = (int *)var_value_str[fn_number];
  609.          fn_command[fn_number] = (void *)CallBack_mdl;
  610.          fn_command_str[fn_number] = (char *) malloc(strlen(cmd)+1);
  611.          strcpy(fn_command_str[fn_number],cmd);
  612.          var_name_str[fn_number] = (char *)malloc(strlen(s2)+1);
  613.          strcpy(var_name_str[fn_number],s2);
  614.          fn_number++;
  615.          continue;
  616.       }
  617.       if(strcmp(s,"STRING_BOX") == 0) {      /* selection box */
  618.          fgetpar(stream,s,' ');         /* get name and position */
  619.          fgetpar(stream,s2,' ');      /* get variable name */
  620.          fgetpar(stream,cmd,10);      /* get command string */
  621.          ExtractStr(s1,s,0,',');
  622.          x = (int) (((float) atoi(s1)) * fx); /* get x position */
  623.          ExtractStr(s1,s,1,',');
  624.          y = (int) (((float) atoi(s1)) * fy); /* get y position */
  625.          ExtractStr(s1,s,2,',');       /* get name */
  626.          add_item(x,y,STRING,s1,0,0);
  627.          var_value_str[fn_number] = (char *)malloc(80); var_value_str[fn_number][0] = 0;
  628.          var_value[fn_number] = (int *)var_value_str[fn_number];
  629.          fn_command[fn_number] = (void *)CallBack_mdl;
  630.          fn_command_str[fn_number] = (char *) malloc(strlen(cmd)+1);
  631.          strcpy(fn_command_str[fn_number],cmd);
  632.          var_name_str[fn_number] = (char *)malloc(strlen(s2)+1);
  633.          strcpy(var_name_str[fn_number],s2);
  634.          fn_number++;
  635.          continue;
  636.       }
  637.       fprintf(stderr,"mdl: unknown command >%s<\n",s);
  638.       c = 0;
  639.       while(c != 10) {            /* try to synchronize */
  640.          if(feof(stream)) {free(cmd); return(0);}
  641.          c = fgetc(stream);
  642.       }
  643.    }
  644.    free(cmd);
  645. }
  646.  
  647. /* -------------------------------------------------------------
  648.    parse a string:
  649.    search for any occurence of $, and try to substitute
  650.    the variable.
  651.    Pass the string to the ksh and read the result
  652.    ------------------------------------------------------------- */
  653. parse(cstr)
  654. char cstr[];
  655. {
  656. int i,n,m,l;
  657. int flg;
  658. char c,s[80],*str,*str2;
  659. char execfile[80];
  660. FILE *stream;
  661.  
  662.    str = (char *) malloc(8192); str2 = (char *) malloc(8192);
  663.    tmpnam_(s);
  664.  
  665. #ifdef AMIGA
  666.    strcpy(execfile,"ram:"); strcat(execfile,s);
  667. #endif
  668.  
  669. #ifdef UNIX
  670.    strcpy(execfile,s);
  671. #endif
  672.    strcpy(str,cstr);
  673.    n = 0; l = strlen(str);
  674.    while(n < l) {
  675.       if(str[n] == '$') {      /* search for variable substitution */
  676.          sgetpar(str,s,n+1,&i);
  677.          flg = 0;
  678.          for(m = 0; m < fn_number; m++) {      /* try to find menu variable */
  679.             if(var_name_str[m] == NULL) continue;
  680.             if(strcmp(s,var_name_str[m]) == 0 ) {   /* variable name found ! */
  681.                flg = 1;
  682.                if((var_funct[m] == STRING) || 
  683.                   (var_funct[m] == SELECTION) ||
  684.                   (var_funct[m] == FILE_SELECT)) {
  685.                   strcpy(s,var_value_str[m]);      /* get replacement value */
  686.                } else {
  687.                   sprintf(s,"%d",var_value_int[m]);
  688.                }
  689.                break;
  690.             }
  691.          }
  692.          if(flg == 0) {       /* try to find internal function */
  693.             if(strcmp(s,"HELP") == 0) {
  694.                flg = 1;
  695.                sgetpar(str,s,i,&i);
  696.                Help(s);
  697.                strcpy(s," ");
  698.             }
  699.             if(strcmp(s,"EXIT") == 0) {
  700. #ifdef AMIGA
  701.                close_gfx(my_window);
  702. #endif
  703.                exit(0);
  704.             }
  705.             if(strcmp(s,"STRINGBOX") == 0) {
  706.                flg = 1;
  707.                sgetpar(str,s,i,&i);
  708.                StringBox(s,s);
  709.             }
  710.             if(strcmp(s,"FILESELECT") == 0) {
  711.                flg = 1;
  712.                sgetpar(str,s,i,&i);
  713.                FileSelect(s,s);
  714.             }
  715.          }
  716.          if(flg == 1) {         /* replace variable name by string */
  717.             m = 0;
  718.             while(i < l) str2[m++] = str[i++];
  719.             str2[m]=0;
  720.             c = 32; m = 0;
  721.             while(s[m] != 0) str[n++] = s[m++];
  722.             str[n]=0;
  723.             strcat(str,str2); l=strlen(str);
  724.          }
  725.       }
  726.       n = n + 1;
  727.    }
  728. /* going to execute command. Output will be directed to a help window */
  729.    if(str[0] == '!') {
  730.       n = 0;
  731.       while(str[n] != 0) {str[n] = str[n+1]; n++;} ; 
  732.       ExecIO(str);
  733.    } else {
  734.       stream = fopen(execfile,"w");
  735.       fprintf(stream,"%s\n",str);
  736.       fclose(stream);
  737.  
  738.       strcpy(str,language);
  739.       strcat(str," ");
  740.       strcat(str,execfile);
  741.       ExecIO(str);
  742.       unlink(execfile);
  743.    }
  744.  
  745.    free(str); free(str2);
  746.    return(0);
  747. }
  748.  
  749. /* add I/O redirection to command, execute command and display output */
  750.  
  751. ExecIO(cmd)
  752. char cmd[];
  753. {
  754. int n,m,l;
  755. char *s;
  756. char *str;
  757. char *resultfile;
  758. FILE *stream;
  759.  
  760. #ifdef AMIGA
  761. struct FileHandle *input, *output;
  762. #endif
  763.  
  764.    str = (char *) malloc(8192);
  765.    resultfile = (char *) malloc(80);
  766.    s = (char *) malloc(80);
  767.  
  768. #ifdef AMIGA
  769.    tmpnam_(s); strcpy(resultfile,"ram:"); strcat(resultfile,s);
  770.  
  771.    n = 0;
  772.    while(cmd[n] != ' ') {str[n] = cmd[n]; n++;}
  773.    str[n] = 0;
  774.    m = 0; l = strlen(cmd);
  775.    while(n <= l) s[m++] = cmd[n++];
  776.    strcat(str," >"); strcat(str,resultfile);
  777.    strcat(str," <CON:30/30/30/30/mdl-hilf");
  778.    strcat(str,s);
  779. input = (struct FileHandle *) Open("nil:",MODE_OLDFILE);
  780. output = (struct FileHandle *) Open("nil:",MODE_NEWFILE);
  781.    Execute(str,input,output);
  782. Close(input);
  783. Close(output);
  784.  
  785.    stream = fopen(resultfile,"r");
  786.    n=fread(str,1,4094,stream); str[n]=0;
  787.    fclose(stream);
  788. #endif
  789.  
  790. #ifdef UNIX
  791.    tmpnam_(resultfile);
  792.    freopen(resultfile,"w",stdout);
  793.    system(cmd);
  794.    freopen(resultfile,"r",stdout);
  795.    n=fread(str,1,8190,stdout); str[n]=0;
  796.    freopen("/dev/syscon","w",stdout);
  797. #endif
  798.  
  799.    unlink(resultfile);
  800.    postparse(str);
  801.    free(s); free(resultfile); free(str);
  802.    return(0);
  803. }
  804.  
  805.  
  806. postparse(str)
  807. char *str;
  808. {
  809. char *s1, *s2, *s3, *s4, *cmd;
  810. int i,m,p;
  811.  
  812.    s1 = (char *) malloc(128);
  813.    s2 = (char *) malloc(128);
  814.    s3 = (char *) malloc(256);
  815.    s4 = (char *) malloc(8192);
  816.    cmd = (char *) malloc(128);
  817.  
  818.    p = 0; s4[0] = 0;
  819.    while(str[p] != 0) {
  820.       i = 0; while(str[p] != '\n') s3[i++] = str[p++];
  821.       s3[i] = 0; p = p + 1;
  822.  
  823.       sscanf(s3,"%s %s %s",cmd,s1,s2);
  824.  
  825.       if(strcmp(cmd,"SET") == 0) { /* SET variable */
  826.          for(m = 0; m < fn_number; m++) {      /* try to find menu variable */
  827.             if(var_name_str[m] == NULL) continue;
  828.             if(strcmp(s1,var_name_str[m]) == 0 ) {   /* variable name found ! */
  829.                if(var_funct[m] == STRING) strcpy(var_value_str[m],s2);
  830.                if(var_funct[m] == TOGGLE) var_value_int[m] = atoi(s2);
  831.                if(var_funct[m] == SCALEX) var_value_int[m] = atoi(s2);
  832.                if(var_funct[m] == SCALEY) var_value_int[m] = atoi(s2);
  833.                if(var_funct[m] == SCALEX11) var_value_int[m] = atoi(s2);
  834.                if(var_funct[m] == SCALEY11) var_value_int[m] = atoi(s2);
  835.                s3[0] = 0;
  836.                break;
  837.             }
  838.          }
  839.       }
  840.       if(strcmp(cmd,"EXIT") == 0) { /* EXIT menu */
  841. #ifdef AMIGA
  842.          close_gfx(my_window);
  843. #endif
  844.          free(cmd); free(s4); free(s3); free(s2); free(s1);
  845.          exit(0);
  846.       }
  847.       if(strcmp(cmd,"CD") == 0) { /* CD */
  848.          changedir(s1);
  849.          free(cmd); free(s4); free(s3); free(s2); free(s1);
  850.          return(0);
  851.       }
  852.       if(s3[0] != 0) {strcat(s4,s3); strcat(s4,"\n");}
  853.    }
  854.    if(s4[0] != 0) Help(s4);
  855.    free(cmd); free(s4); free(s3); free(s2); free(s1);
  856. }
  857.  
  858.  
  859. changedir(str)
  860. char *str;
  861. {
  862. #ifdef AMIGA
  863. struct FileLock *pwd;
  864.  
  865.       pwd = (struct FileLock *) Lock(str,-2);
  866.       if(pwd != NULL) {
  867.          pwd = (struct FileLock *) CurrentDir(pwd);
  868.          UnLock(pwd);
  869.       }
  870. #endif
  871. #ifdef UNIX
  872.       chdir(str);
  873. #endif
  874. return(0);
  875. }
  876.  
  877.  
  878. checkopt(argc,argv,s,sv)
  879. int argc;
  880. char *argv[],s[],sv[];
  881. {
  882. int   n,erg;
  883.  
  884.    erg=FALSE;
  885.    for(n=1;n<argc;n++) {
  886.       if(strcmp(argv[n],s)==0) {
  887.          erg=TRUE;
  888.          strcpy(sv,argv[n+1]);
  889.       }
  890.    }
  891.    return(erg);
  892. }
  893.  
  894. #ifdef AMIGA
  895. amifork(str)
  896. char *str;
  897. {
  898. struct FileHandle *input, *output;
  899. char *s;
  900.  
  901. s = (char *) malloc(256);
  902. strcpy(s,"run >nil: <nil: "); strcat(s,str);
  903. input = (struct FileHandle *) Open("nil:",MODE_OLDFILE);
  904. output = (struct FileHandle *) Open("nil:",MODE_NEWFILE);
  905.    Execute(s,input,output);
  906. Close(input);
  907. Close(output);
  908. free(s);
  909. return(0);
  910. }
  911. #endif
  912.